/*
 *  Copyright 2011 Mark Eschbach
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 * $HeadURL: http://phunctional-system-integration.googlecode.com/svn/tags/psi-reactor-2.4/trunk/psi-util/src/main/java/com/meschbach/psi/util/rest/PostRequest.java $
 * $Id: PostRequest.java 242 2011-03-07 02:48:48Z marioandrest@gmail.com $
 */
package com.meschbach.psi.util.rest;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;

/**
 * A <code>PostRequest</code> is a request builder for sending HTTP POST verbs
 * to the a remote URI, possibly with a given body.
 *
 * @author "Mark Eschbach" meschbach@gmail.com
 * @version 1.0.0
 * @since 2.4.0
 */
public class PostRequest implements RequestBuilder {

    private String entity;

    public PostRequest() {
        this(null);
    }

    /**
     * Constructs a new PostRequest with the given entity.  If the input entity
     * is null then no HTTP entity will be attached.
     * 
     * @param entity is the entity 
     */
    public PostRequest(String entity) {
        this.entity = entity;
    }

    /**
     * @see RequestBuilder#buildRequest(java.net.URI) 
     */
    public HttpUriRequest buildRequest(URI resource) {
        HttpPost post = new HttpPost(resource);
        if (entity != null) {
            try {
                post.setEntity(new StringEntity(entity));
            } catch (UnsupportedEncodingException uee) {
                throw new IllegalStateException(uee);
            }
        }
        return post;
    }
}
